//
//  DMCommonwealViewController.m
//  CQKuaiYi
//
//  Created by Mr.Cannon on 2017/1/9.
//  Copyright © 2017年 Mr.Cannon. All rights reserved.
//

#import "DMCommonwealViewController.h"

@interface DMCommonwealViewController ()
//WebView
@property(nonatomic,strong)UIWebView *webView;
//NSURLSession
@property (nonatomic,strong)NSURLSession *session;

@end

@implementation DMCommonwealViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   self.title = @"公益活动";
    self.view.backgroundColor = [UIColor cz_randomColor];
}
-(void)setupUI
{
    UIWebView* webView = [[UIWebView alloc]initWithFrame:self.view.bounds];
    [self.webView setBackgroundColor:DMRGBCOLOR];
    self.webView = webView;
    [self.view addSubview:webView];
    //加载数据
    [self loadData];
}
#pragma mark - 加载数据
-(void)loadData
{
    NSURL* url = [NSURL URLWithString:@"http://202.106.210.115:18080/hyde-pluto-h360/nt/appEntrance.htm"];
    //弹框提醒
    [DMTools dmShowSVTitle:@"加载中" andTime:1];
    NSURLSessionDataTask* dataTask = [self.session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if(error == nil && data != nil && data.length > 0)
        {
            //反序列化
            NSString* CommonwealHtml = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
            [[NSOperationQueue mainQueue]addOperationWithBlock:^{
                [self.webView loadHTMLString:CommonwealHtml baseURL:url];
            }];
        }else
        {
            NSLog(@"错误信息:%@",error);
        }
    }];
    //启动任务
    [dataTask resume];
}
#pragma mark - 懒加载
- (NSURLSession *)session{
    if (_session == nil) {
        NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
        //设置配置信息
        config.HTTPAdditionalHeaders = @{
                                         @"User-Agent":@"iPhone"
                                         };
        _session = [NSURLSession sessionWithConfiguration:config];
        
    }
    return _session;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
